﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Security;
using Web.Context;
using RentalSystem.Data.EF;
using RentalSystem.Data.EF.Core;
using Web.Models;
using Web.Helper;
using System.Web.Caching;

namespace Web.Controllers
{
    public class BaseController : Controller
    {
        public BaseController()
        {
            FillMainPageData();
        }

        #region LayoutContents

        private void FillMainPageData()
        {
            if (ViewBag.StatsMovies == null)
            {
                Dictionary<string, string> stats = CurrentContext.Context.CurrentCache.StatisticsGet();
                if (stats == null)
                {
                    List<Statistics> list = DataContext.StatisticsDao.List();
                    if (list != null)
                    {
                        stats = new Dictionary<string, string>();
                        foreach (Statistics item in list)
                        {
                            if (item.StatisticName == EntityManager.Stats_MoviesTotalTime)
                            {
                                float totalTime = ((long)item.StatisticValue) / (60 * 1.0f);
                                stats.Add(item.StatisticName, Math.Floor(totalTime).ToString());
                            }
                            else
                            {
                                stats.Add(item.StatisticName, item.StatisticValue.ToString());
                            }
                        }
                        CurrentContext.Context.CurrentCache.StatisticsAdd(stats);
                    }
                }

                ViewBag.StatsMovies = stats["MoviesTotalCount"];
                ViewBag.StatsUsers = stats["UsersTotalCount"];
                ViewBag.StatsTimes = stats["MoviesTotalTime"];
                ViewBag.StatsRentals = stats["RentalsTotalCount"]; ;
            }

            if (IsUserAuthenticated)
            {
                ViewBag.UserName = "Zalogowany: " + CurrentUser.DisplayName;

                if (CurrentUser.RoleId == 1)
                    ViewBag.UserCardLink = String.Format("<a href ='/customers/' id='personCard'>Konto</a>");
                else
                    ViewBag.UserCardLink = String.Format("<a href ='/workers/' id='personCard'>Konto</a>");

                ViewBag.UserAction = "<a href ='/account/logout' id='logout'>Wyloguj</a>";
                ViewBag.AccountType = CurrentUser.Type;
                ViewBag.Discount = (int)(CurrentUser.Type) * SettingsManager.UserDiscountPerLevelInPercentage + "%";
            }
            else
            {
                ViewBag.UserName = "Nie jesteś zalogowany";
                ViewBag.UserCardLink = String.Format("<a href ='/account/register' id='personCard'>Załóż konto</a>");
                ViewBag.UserAction = "<a href ='/account/login' id='login'>Zaloguj się</a>";
            }
        }

        public void SetFormMode(Mode mode)
        {
            ViewBag.FormMode = mode;
        }

        #endregion

        #region Properties

        public IDataContext DataContext
        {
            get { return CurrentContext.Context.DataContext; }
        }

        public User CurrentUser
        {
            get { return CurrentContext.Context.CurrentUser; }
        }

        public bool IsUserAuthenticated
        {
            get { return CurrentUser != null; }
        }

        public SecurityManager SecurityManager
        {
            get { return CurrentContext.Context.SecurityManager; }
        }

        public LogsManager LogManager
        {
            get { return CurrentContext.Context.LogsManager; }
        }

        public SettingsManager SettingsManager
        {
            get { return CurrentContext.Context.SettingsManager; }
        }

        #endregion

        #region MessagesManager

        public void ShowErrorOnAnotherPage(string error)
        {
            TempData["errors"] = error;
        }

        public void ShowSuccessOnAnotherPage(string success)
        {
            TempData["success"] = success;
        }

        public void LoadErrorsAndSuccessFromAnotherPage()
        {
            if(TempData["errors"] != null)
                ShowError(TempData["errors"] as string);

            if (TempData["success"] != null)
                ShowSuccess(TempData["success"] as string);
        }

        public void ShowSuccess(string message)
        {
            ViewBag.Success = message;
        }

        public void ShowError(string error)
        {
            ViewBag.Errors = error;
        }
        
        #endregion
    }
}
